/*
 * IR_sensor.c
 *
 * Created: 11/10/2013 2:40:40 PM
 *  Author: Daniel
 */ 
#include <stdio.h>
#include <avr/io.h>
#include <string.h>
#include "lcd.h"
#include "util.h"
#include <math.h>

/** initializes the ADC
*   initializes the ADMUX and ADCSRA. also set ADMUX to read from channel two.                                                          
*/

void ADC_init(){ //initializes the analog to digital conversion
	//reference voltage, alignment, channel
	ADMUX = 0b11000010;
	//Enable, running mode, interrupt
	// and clock select
	ADCSRA = 0b10000111 ;
	ADMUX |= (0x02 & 0x1F); //select channel
}

/** gets an ADC
*       starts ADC sampling then waits till completion 
*		Return The value of ADCW
*/

unsigned int ADC_read(){ // collects the ADC data and returns it
	//set up channel
// 	
// 	ADCSRA |=  (1 << ADSC); //Start ADC sampling prescaler is 128
// 	while ((ADCSRA & (1<<ADSC))) {   //Wait until done
// 		
// 	}
// 	return ADCW;

	asm volatile (
	/* ADCSRA |= (1 << ADSC) */
		"in r24, %[adcsra]	 \n\t"
		"ldi r25, %[adscval] \n\t"
		"or  r24, r25		 \n\t"
		"out %[adcsra],r24	 \n\t"

	/* while(ADSCRA & (1 << ADSC)) ; */
		"loop_%=:			 \n\t"
		"in r24, %[adcsra]   \n\t"
		"and r24, r25		 \n\t"
		"cpi r24, 0		 	 \n\t"
		"brne loop_%=		 \n\t"

	/* return ADCW */
		"in r24, %[adcl]	 \n\t"
		"in r25, %[adch]	 \n\t"

	: /* no inputs */
	: [adcsra] "M"  (_SFR_IO_ADDR(ADCSRA)),
	  [adscval] "M" (1 << ADSC),
	  [adcl] "M"	(_SFR_IO_ADDR(ADCL)),
	  [adch] "M"	(_SFR_IO_ADDR(ADCH))
	: "r24",
	  "r25",
	  "memory"
	);
}
/** calculates the distance using the ADC reading
*	it decides what the IR distance is by the returned value from ADC_read. 
	Which is ran through 50 case statement to decide the distance
*   return distance
*/
 unsigned int IR_cal(){
	ADC_init();
	float sum =0;
	int distance =0;
	int count =0;
	for(int i =0; i<3; i++){
		count += ADC_read(0x02);// read in voltage on channel 2
	}	
	sum = (count/3);
	
	if (sum >= 988)
	{
		distance = 9;
	}
	else if ((sum >= 923) && (sum < 988))
	{
		distance = 10;
	}
	else if ((sum >= 855) && (sum < 923))
	{
		distance = 11;
	}
	else if ((sum >= 797) && (sum < 855))
	{
		distance = 12;
	}
	else if ((sum >= 752) && (sum < 797))
	{
		distance = 13;
	}
	else if ((sum >= 707) && (sum < 752))
	{
		distance = 14;
	}
	else if ((sum >= 667) && (sum < 707))
	{
		distance = 15;
	}
	else if ((sum >= 625) && (sum < 667))
	{
		distance = 16;
	}
	else if ((sum >= 594) && (sum < 625))
	{
		distance = 17;
	}
	else if ((sum >= 569) && (sum < 594))
	{
		distance = 18;
	}
	else if ((sum >= 540) && (sum < 569))
	{
		distance = 19;
	}
	else if ((sum >= 517) && (sum < 540))
	{
		distance = 20;
	}
	else if ((sum >= 494) && (sum < 517))
	{
		distance = 21;
	}
	else if ((sum >= 477) && (sum < 494))
	{
		distance = 22;
	}
	else if ((sum >= 465) && (sum < 477))
	{
		distance = 23;
	}
	else if ((sum >= 450) && (sum < 465))
	{
		distance = 24;
	}
	else if ((sum >= 428) && (sum < 450))
	{
		distance = 25;
	}
	else if ((sum >= 408) && (sum < 428))
	{
		distance = 26;
	}
	else if ((sum >= 392) && (sum < 408))
	{
		distance = 27;
	}
	else if ((sum >= 381) && (sum < 392))
	{
		distance = 28;
	}
	else if ((sum >= 371) && (sum < 381))
	{
		distance = 29;
	}
	else if ((sum >= 357) && (sum < 371))
	{
		distance = 30;
	}
	else if ((sum >= 345) && (sum < 357))
	{
		distance = 31;
	}
	else if ((sum >= 335) && (sum < 345))
	{
		distance = 32;
	}
	else if ((sum >= 325) && (sum < 335))
	{
		distance = 33;
	}
	else if ((sum >= 315) && (sum < 325))
	{
		distance = 34;
	}
	else if ((sum >= 306) && (sum < 315))
	{
		distance = 35;
	}
	else if ((sum >= 299) && (sum < 306))
	{
		distance = 36;
	}
	else if ((sum >= 291) && (sum < 299))
	{
		distance = 37;
	}
	else if ((sum >= 285) && (sum < 291))
	{
		distance = 38;
	}
	else if ((sum >= 281) && (sum < 285))
	{
		distance = 39;
	}
	else if ((sum >= 275) && (sum < 281))
	{
		distance = 40;
	}
	else if ((sum >= 268) && (sum < 275))
	{
		distance = 41;
	}
	else if ((sum >= 261) && (sum < 268))
	{
		distance = 42;
	}
	else if ((sum >= 256) && (sum < 261))
	{
		distance = 43;
	}
	else if ((sum >= 253) && (sum < 256))
	{
		distance = 44;
	}
	else if ((sum >= 247) && (sum < 253))
	{
		distance = 45;
	}
	else if ((sum >= 243) && (sum < 247))
	{
		distance = 46;
	}
	else if ((sum >= 238) && (sum < 243))
	{
		distance = 47;
	}
	else if ((sum >= 230) && (sum < 238))
	{
		distance = 48;
	}
	else if ((sum >= 226) && (sum < 230))
	{
		distance = 49;
	}
	else if ((sum >= 223) && (sum < 226))
	{
		distance = 50;
	}
	else if ((sum >= 197) && (sum < 223))
	{
		distance = 55;
	}
	else if ((sum >= 180) && (sum < 197))
	{
		distance = 60;
	}
	else{
		distance = 99;
	}

	
	return distance;
}